package controller;


import java.io.File;
import java.net.InetAddress;
import java.util.Hashtable;

import javax.swing.DefaultListModel;

import model.FileMessage;
import cs.remoteapp.FileAnswer;
import cs.remoteapp.TextMessage;
import main.ChatController;
import model.Conversation;
import model.User;

/**
 * Class representing the Connected state
 * implement some of the methods of state
 * Package  controller
 * Project ChatSystemG5 - INSA 4INFOA1 - 2013/2014
 * @author Thomas Vuillemin Martin Conant
 */
public class Connected extends State {
	
	/**
	 * Basic constructor of Connected
	 * @param controller
	 */
	public Connected(ChatController controller) {
		super(controller);
	}
	
	/**
	 * Change the state to disconnect.
	 * Reinitializate the conversations and connectedUsers on the model
	 */
	public void disconnect() {
		changeState(new Disconnected(controller));
		controller.getModel().setState(DISCONNECTED);
		//we reinitializate the conversations and connectedUsers
		controller.getModel().getConnectedUsers().clear();
		controller.getModel().getConversations().clear();
	}
	
	/**
	 * Send a TextMessage
	 * @param text to be send
	 * @param adresee
	 */
	public void sendText (String text, User adresee) {
		System.out.println("I'm going to send a message\n");
		TextMessage message = new TextMessage(controller.getModel().getLocalUser().getName(), text);
		controller.getModel().addMessageToConversation(message, adresee.getIpAdress());
	}
	
	/**
	 * had a FileMessage to the conversation. 
	 */
	public void sendFile(File file, User adresee) {
		FileMessage fileMessage = new FileMessage(controller.getModel().getLocalUser().getName(), file);
        System.out.println("I am gonna send a file\n");
		controller.getModel().addMessageToConversation(fileMessage, adresee.getIpAdress());
	}
	

	public void selectFile (String fileAdress) {
	}
	public void openFile (int downloadKey) {
	}
	public void saveFile (int downloadKey, String path) {
	}
	
	/**
	 * Had a file answer with true answer to the right conversation
	 */
	public void acceptFile (String fileName, User adresee) {
        System.out.println("Accept file\n");
		FileAnswer fileAnswer = new FileAnswer(controller.getModel().getLocalUser().getName(), fileName, true);
		controller.getModel().addMessageToConversation(fileAnswer, adresee.getIpAdress());
	}
	
	/**
	 * Had a file answer with false answer to the right conversation
	 */
	public void refuseFile (String fileName, User adresee) {    
		System.out.println("Refuse file\n");
		FileAnswer fileAnswer = new FileAnswer(controller.getModel().getLocalUser().getName(), fileName, false);
		controller.getModel().addMessageToConversation(fileAnswer, adresee.getIpAdress());
	}
	public void addAdressee (User adresee) {
	}
	public void removeAdresee (User adresee) {
	}
	
	/**
	 * Called when we want to start a conversation with a user
	 * open a conversation
	 */
	public void openNewChatWindow (User adresee) {
        //we create a Conversation with this user and add it 
        controller.getModel().openConversation(adresee);
	}
}
